<?php
/**
 * 登陆
 * 
 * @version $Id$
 */

class Module_login
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}
	
	/**
	 * 登陆
	 */
	function in() {
		$mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : null;
		$password = isset($_POST['password']) ? intval($_POST['password']) : null;
		
		// 检查手机号码有效性
		if(empty($mobile) && preg_match('%^[0-9]{13}$%' , $mobile)) {
			return View::error('没有填写手机号码，或手机号码填写有误');
		}
		
		// 检查密码有效性
		if($password === null || strlen($password) == 0) {
			return View::error('没有填写密码');
		}
		
		loadLib('User');
		$result = User::checkPassword($mobile , $password);
		
		if(empty($result)) {
			return View::error('密码错误');
		}
		
		$uin = $result['uin'];
		
		$session = loadLib('Session');
		$session->addSkey($uin);
		
		// 写cookie
		setcookie('sid' , $session->getSkey() , time() + (86400 * 30) , '/' , SITEHOST);
		
		$smarty = loadSmarty();
		
		// 重新赋值
		$smarty->assign('skey' , $session->getSkey());
		$smarty->assign('uin' , $session->getUin());
		
		//return $smarty->fetch('login/in.tpl.php');
		header('Location: ' . SITEURL . '/app.php?m=overlordguide&sid=' . sessionInfo('skey') );
	}
	
	/**
	 * 退出
	 */
	function out() {
		$session = loadLib('Session');
		// $session->deleteSkey();
		
		$smarty = loadSmarty();
		
		// 重新赋值
		$smarty->assign('skey' , $session->getSkey());
		$smarty->assign('uin' , $session->getUin());
		
		// delete cookie
		if(isset($_COOKIE['sid'])) {
			setcookie ('sid' , '' , time() - 3600 , '/' , SITEHOST);
		}
		
		return $smarty->fetch('login/out.tpl.php');
	}
	/**
	 * 点击游戏链接
	 */
	function m(){
		$game = isset($_GET['game']) ? trim($_GET['game']) : null;
		
		$smarty = loadSmarty();
		$smarty->assign('game' , $game);
		
		return $smarty->fetch('login/newForm.tpl.php');
	}
	/**
	 * 找回密码导航
	 */
	function findpassword(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('login/findpassword.tpl.php');
	}
}

# end of script
